package com.java110.mall.shop.smo.impl;


import com.java110.core.factory.GenerateCodeFactory;
import com.java110.dto.account.AccountDto;
import com.java110.dto.integral.IntegralDto;
import com.java110.intf.acct.IAccountInnerServiceSMO;
import com.java110.intf.acct.IIntegralV1InnerServiceSMO;
import com.java110.intf.shop.IShopSmsV1InnerServiceSMO;
import com.java110.po.account.AccountPo;
import com.java110.po.integral.IntegralPo;
import com.java110.po.shop.ShopPo;
import com.java110.mall.shop.dao.IShopServiceDao;
import com.java110.intf.shop.IShopInnerServiceSMO;
import com.java110.dto.shop.ShopDto;
import com.java110.po.shopSms.ShopSmsPo;
import com.java110.utils.constant.StatusConstant;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.core.base.smo.BaseServiceSMO;
import com.java110.dto.PageDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName FloorInnerServiceSMOImpl
 * @Description 店铺内部服务实现类
 * @Author wuxw
 * @Date 2019/4/24 9:20
 * @Version 1.0
 * add by wuxw 2019/4/24
 **/
@RestController
public class ShopInnerServiceSMOImpl extends BaseServiceSMO implements IShopInnerServiceSMO {

    @Autowired
    private IShopServiceDao shopServiceDaoImpl;

    @Autowired
    private IAccountInnerServiceSMO accountInnerServiceSMOImpl;

    @Autowired
    private IIntegralV1InnerServiceSMO integralV1InnerServiceSMOImpl;

    @Autowired
    private IShopSmsV1InnerServiceSMO shopSmsV1InnerServiceSMOImpl;

    @Override
    public int saveShop(@RequestBody ShopPo shopPo) {
        int saveFlag = 1;
        shopServiceDaoImpl.saveShopInfo(BeanConvertUtil.beanCovertMap(shopPo));

        //todo 创建 商铺账户信息

        AccountPo accountPo = new AccountPo();
        accountPo.setAmount("0");
        accountPo.setAcctId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_acctId));
        accountPo.setObjId(shopPo.getShopId());
        accountPo.setObjType(AccountDto.OBJ_TYPE_STORE);
        accountPo.setAcctType(AccountDto.ACCT_TYPE_CASH);
        accountPo.setAcctName(shopPo.getShopName());
        accountPo.setPartId(shopPo.getStoreId());
        accountInnerServiceSMOImpl.saveAccount(accountPo);

        IntegralPo integralPo = new IntegralPo();
        integralPo.setIntegralName(shopPo.getShopName());
        integralPo.setAmount("0");
        integralPo.setObjId(shopPo.getShopId());
        integralPo.setLink(shopPo.getReturnLink());
        integralPo.setIntegralId(GenerateCodeFactory.getGeneratorId("11"));
        integralPo.setObjType(IntegralDto.OBJ_TYPE_STORE);
        integralV1InnerServiceSMOImpl.saveIntegral(integralPo);
        // 店铺短信
        ShopSmsPo shopSmsPo = new ShopSmsPo();
        shopSmsPo.setSmsCount("0");
        shopSmsPo.setShopName(shopPo.getShopName());
        shopSmsPo.setShopId(shopPo.getShopId());
        shopSmsPo.setSmsSwitch("ON");
        shopSmsPo.setSmsId(GenerateCodeFactory.getGeneratorId("11"));
        shopSmsV1InnerServiceSMOImpl.saveShopSms(shopSmsPo);
        return saveFlag;
    }

    @Override
    public int updateShop(@RequestBody ShopPo shopPo) {
        int saveFlag = 1;
        shopServiceDaoImpl.updateShopInfo(BeanConvertUtil.beanCovertMap(shopPo));
        return saveFlag;
    }

    @Override
    public int deleteShop(@RequestBody ShopPo shopPo) {
        int saveFlag = 1;
        shopPo.setStatusCd("1");
        shopServiceDaoImpl.updateShopInfo(BeanConvertUtil.beanCovertMap(shopPo));
        return saveFlag;
    }

    @Override
    public List<ShopDto> queryShops(@RequestBody ShopDto shopDto) {

        //校验是否传了 分页信息

        int page = shopDto.getPage();

        if (page != PageDto.DEFAULT_PAGE) {
            shopDto.setPage((page - 1) * shopDto.getRow());
        }

        List<ShopDto> shops = BeanConvertUtil.covertBeanList(shopServiceDaoImpl.getShopInfo(BeanConvertUtil.beanCovertMap(shopDto)), ShopDto.class);

        return shops;
    }

    @Override
    public List<ShopDto> getOrderShopInfo(@RequestBody String[] shopIds) {

        Map shopIdInfo = new HashMap();
        shopIdInfo.put("statusCd", StatusConstant.STATUS_CD_VALID);
        shopIdInfo.put("shopIds", shopIds);

        List<ShopDto> shops = BeanConvertUtil.covertBeanList(shopServiceDaoImpl.getOrderShopInfo(shopIdInfo), ShopDto.class);

        return shops;
    }

    @Override
    public int queryShopsCount(@RequestBody ShopDto shopDto) {
        return shopServiceDaoImpl.queryShopsCount(BeanConvertUtil.beanCovertMap(shopDto));
    }

    @Override
    public int returnStoreOrder(@RequestBody ShopDto shopDto) {
        return 0;
    }

    public IShopServiceDao getShopServiceDaoImpl() {
        return shopServiceDaoImpl;
    }

    public void setShopServiceDaoImpl(IShopServiceDao shopServiceDaoImpl) {
        this.shopServiceDaoImpl = shopServiceDaoImpl;
    }
}
